package com.smalls.redshoes.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.internal.view.SupportMenu;
import android.support.v7.widget.helper.ItemTouchHelper.Callback;

import com.smalls.redshoes.util.BitmapUtils.BitmapCallBack;

import org.apache.http.HttpStatus;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class BitmapCache {
    public final String CACHE_FILE = "images";
    private FileUtils fileUtils = new FileUtils();
    private boolean iso = false;
    private List<Bitmap> listCache;

    public BitmapCache() {
        if (!this.fileUtils.isFileExist("images")) {
            this.fileUtils.createSDDir("images");
        }
        if (this.listCache == null) {
            this.listCache = new ArrayList();
            return;
        }
        for (int i = 0; i < this.listCache.size(); i++) {
            ((Bitmap) this.listCache.get(i)).recycle();
        }
        this.listCache.clear();
    }

    public void getBitmap(final String url, int position, final BitmapCallBack bitmapCallBack) {
        final Handler handler = new Handler() {
            public void handleMessage(Message message) {
                bitmapCallBack.load((Bitmap) message.obj);
            }
        };
        if (ThreadPoolManager.cachePool != null && !ThreadPoolManager.cachePool.isShutdown()) {
            ThreadPoolManager.cachePool.execute(new Thread(new Runnable() {
                public void run() {
                    Bitmap bitmap = BitmapCache.this.getLocalImage(BitmapCache.this.getFilePath(url));
                    if (bitmap == null) {
                        bitmap = BitmapCache.this.downBitmap(url);
                        if (bitmap != null) {
                            BitmapCache.this.listCache.add(bitmap);
                            handler.obtainMessage(0, bitmap).sendToTarget();
                            return;
                        }
                        return;
                    }
                    BitmapCache.this.listCache.add(bitmap);
                    handler.obtainMessage(0, bitmap).sendToTarget();
                }
            }));
        }
    }

    public String getFilePath(String url) {
        return "images/" + url.substring(url.lastIndexOf("/") + 1);
    }

    public String getFileName(String url) {
        return url.substring(url.lastIndexOf("/") + 1);
    }

    public void clearBitmap() {
        if (this.listCache != null) {
            for (int i = 0; i < this.listCache.size(); i++) {
                ((Bitmap) this.listCache.get(i)).recycle();
            }
            this.listCache.clear();
        }
        this.iso = false;
    }

    public void deleteFile() {
        this.fileUtils.deleteDir(new File(Environment.getExternalStorageDirectory() + "/" + "images"));
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public android.graphics.Bitmap downBitmap(java.lang.String r11) {
        /*
        r10 = this;
        r9 = 3;
        r6 = new android.graphics.BitmapFactory$Options;
        r6.<init>();
        r8 = android.graphics.Bitmap.Config.RGB_565;
        r6.inPreferredConfig = r8;
        r4 = 0;
        r2 = 0;
        r1 = 0;
        r7 = new java.net.URL;	 Catch:{ Exception -> 0x0062 }
        r7.<init>(r11);	 Catch:{ Exception -> 0x0062 }
        r8 = r7.openConnection();	 Catch:{ Exception -> 0x0062 }
        r0 = r8;
        r0 = (java.net.HttpURLConnection) r0;	 Catch:{ Exception -> 0x0062 }
        r2 = r0;
        r8 = 10000; // 0x2710 float:1.4013E-41 double:4.9407E-320;
        r2.setConnectTimeout(r8);	 Catch:{ Exception -> 0x0062 }
        r4 = r2.getInputStream();	 Catch:{ Exception -> 0x0062 }
        r8 = 3;
        r5 = copyInputStream(r4, r8);	 Catch:{ Exception -> 0x0062 }
        if (r5 == 0) goto L_0x005b;
    L_0x002a:
        r8 = r5.size();	 Catch:{ Exception -> 0x0062 }
        if (r8 != r9) goto L_0x005b;
    L_0x0030:
        r8 = 1;
        r6.inJustDecodeBounds = r8;	 Catch:{ Exception -> 0x0062 }
        r8 = 0;
        r8 = r5.get(r8);	 Catch:{ Exception -> 0x0062 }
        r8 = (java.io.InputStream) r8;	 Catch:{ Exception -> 0x0062 }
        r9 = 0;
        android.graphics.BitmapFactory.decodeStream(r8, r9, r6);	 Catch:{ Exception -> 0x0062 }
        r8 = 200; // 0xc8 float:2.8E-43 double:9.9E-322;
        r9 = 250; // 0xfa float:3.5E-43 double:1.235E-321;
        r10.sampleSize(r6, r8, r9);	 Catch:{ Exception -> 0x0062 }
        r8 = 1;
        r8 = r5.get(r8);	 Catch:{ Exception -> 0x0062 }
        r8 = (java.io.InputStream) r8;	 Catch:{ Exception -> 0x0062 }
        r9 = 0;
        r1 = android.graphics.BitmapFactory.decodeStream(r8, r9, r6);	 Catch:{ Exception -> 0x0062 }
        r8 = 2;
        r8 = r5.get(r8);	 Catch:{ Exception -> 0x0062 }
        r8 = (java.io.InputStream) r8;	 Catch:{ Exception -> 0x0062 }
        r10.saveBitmap(r11, r8);	 Catch:{ Exception -> 0x0062 }
    L_0x005b:
        r10.closeIO(r4);
        r2.disconnect();
    L_0x0061:
        return r1;
    L_0x0062:
        r3 = move-exception;
        r3.printStackTrace();	 Catch:{ all -> 0x006d }
        r10.closeIO(r4);
        r2.disconnect();
        goto L_0x0061;
    L_0x006d:
        r8 = move-exception;
        r10.closeIO(r4);
        r2.disconnect();
        throw r8;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.smalls.redshoes.util.BitmapCache.downBitmap(java.lang.String):android.graphics.Bitmap");
    }

    public void saveBitmap(final String url, final InputStream inputStream) {
        ThreadPoolManager.startSavePool(new Thread(new Runnable() {
            public void run() {
                String fileName = "images/" + BitmapCache.this.getFileName(url);
                if (!BitmapCache.this.fileUtils.isFileExist(fileName)) {
                    BitmapCache.this.fileUtils.write2SDFromInput(fileName, inputStream);
                }
            }
        }));
    }

    public Bitmap getLocalImage(String path) {
        Options options = new Options();
        options.inPreferredConfig = Config.RGB_565;
        if (!this.fileUtils.isFileExist(path)) {
            return null;
        }
        Bitmap dump = null;
        try {
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(this.fileUtils.getSDPATH() + path, options);
            sampleSize(options, HttpStatus.SC_OK, Callback.DEFAULT_SWIPE_ANIMATION_DURATION);
            return BitmapFactory.decodeFile(this.fileUtils.getSDPATH() + path, options);
        } catch (OutOfMemoryError oom) {
            oom.printStackTrace();
            return dump;
        }
    }

    public void closeIO(Closeable io) {
        if (io != null) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void sampleSize(Options options, int w, int h) {
        options.inJustDecodeBounds = false;
        if (w <= 0 || h <= 0) {
            options.inSampleSize = 1;
        }
        int initialSize = Math.max((int) Math.ceil(Math.sqrt((double) ((options.outWidth * options.outHeight) / (w * h)))), Math.max(options.outWidth / w, options.outHeight / h));
        if (initialSize < 1) {
            options.inSampleSize = 1;
        } else {
            options.inSampleSize = initialSize;
        }
        if (initialSize < 1) {
            options.inSampleSize = 1;
        } else {
            options.inSampleSize = initialSize;
        }
    }

    public Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.RGB_565);
        Canvas canvas = new Canvas(output);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(SupportMenu.CATEGORY_MASK);
        canvas.drawRoundRect(rectF, 20.0f, 20.0f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
}
